package logic

import (
	"context"
	"fmt"
	"gitservice/internal/pkg"
	"strings"

	"gitservice/internal/svc"
	"gitservice/internal/types"

	"github.com/tal-tech/go-zero/core/logx"
)

type GitUpgradeLogic struct {
	log    logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewGitUpgradeLogic(ctx context.Context, svcCtx *svc.ServiceContext) GitUpgradeLogic {
	return GitUpgradeLogic{
		log:    logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *GitUpgradeLogic) GitUpgrade(req types.Request) (*types.Response, error) {
	if !strings.Contains(req.Ref, "release") {
		return &types.Response{
			Message: "not release version",
		}, nil
	}

	l.log.Infof("Receive release version %s ...",req.Repository)

	name := req.Repository.Name
	version := req.Ref[strings.Index(req.Ref,"release"):]

	pkg.GitPull(name,req.Repository.Url)
	pkg.GitCheckout(name,version)


	ret := fmt.Sprintf("name: %s , version : %s", name, version)
	return &types.Response{
		Message: ret,
	}, nil
}
